package 领扣算法.力扣计划.算法基础.岛屿数量;

public class Main {
    public int numIslands(char[][] grid) {
        char[][] data=new char[grid.length+2][grid[0].length+2];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                data[i+1][j+1]=grid[i][j];
            }
        }
        int res=0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                if(data[i][j]=='1'){
                  res++;
                  plus(data,i,j);
                }
            }
        }
        return res;
    }

    private void plus(char[][] data, int i, int j) {
        data[i][j]='2';
        if(data[i-1][j]=='1'){
            plus(data,i-1,j);
        }
        if(data[i+1][j]=='1'){
            plus(data,i+1,j);
        }
        if(data[i][j-1]=='1'){
            plus(data,i,j-1);
        }
        if(data[i][j+1]=='1'){
            plus(data,i,j+1);
        }
    }
}
